package com.ruoyi.teaching.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.ExceptionUtil;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.teaching.domain.*;
import com.ruoyi.teaching.mapper.*;
import com.ruoyi.teaching.util.ExportExcelUtil;
import com.ruoyi.teaching.util.FileUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teaching.service.IStageInvestigateDetailService;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * 阶段问卷细节Service业务层处理
 *
 * @author sunshine
 * @date 2024-02-15
 */
@Service
public class StageInvestigateDetailServiceImpl implements IStageInvestigateDetailService
{
    @Autowired
    private StageInvestigateDetailMapper stageInvestigateDetailMapper;

    @Autowired
    private StageInvestigateMapper stageInvestigateMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ExamDetailMapper examDetailMapper;
    /**
     * 查询阶段问卷细节
     *
     * @param id 阶段问卷细节主键
     * @return 阶段问卷细节
     */
    @Override
    public StageInvestigateDetail selectStageInvestigateDetailById(Long id)
    {
        return stageInvestigateDetailMapper.selectStageInvestigateDetailById(id);
    }

    /**
     * 查询阶段问卷细节列表
     *
     * @param stageInvestigateDetail 阶段问卷细节
     * @return 阶段问卷细节
     */
    @Override
    public List<StageInvestigateDetail> selectStageInvestigateDetailList(StageInvestigateDetail stageInvestigateDetail)
    {
        stageInvestigateDetail.setStudentName(SecurityUtils.getUsername());
        return stageInvestigateDetailMapper.selectStageInvestigateDetailList(stageInvestigateDetail);
    }

    /**
     * 新增阶段问卷细节
     *
     * @param stageInvestigateDetail 阶段问卷细节
     * @return 结果
     */
    @Override
    public int insertStageInvestigateDetail(StageInvestigateDetail stageInvestigateDetail)
    {
        stageInvestigateDetail.setCreateTime(DateUtils.getNowDate());
        return stageInvestigateDetailMapper.insertStageInvestigateDetail(stageInvestigateDetail);
    }

    /**
     * 修改阶段问卷细节
     *
     * @param stageInvestigateDetail 阶段问卷细节
     * @return 结果
     */
    @Override
    public int updateStageInvestigateDetail(StageInvestigateDetail stageInvestigateDetail)
    {

        Long id = stageInvestigateDetail.getId();
        StageInvestigateDetail stageInvestigateDetail1 = stageInvestigateDetailMapper.selectStageInvestigateDetailById(id);
        Long stageInvestigateId = stageInvestigateDetail1.getStageInvestigateId();
        List<Long> ids = stageInvestigateDetailMapper.selectId();
        if (ids.isEmpty()){
            stageInvestigateMapper.updateStageInvestigateById(stageInvestigateId);
        }else {
            for (int i = 0; i < ids.size(); i++) {
                if (id.equals(ids.get(i))){
                    stageInvestigateMapper.updateStageInvestigateCount(stageInvestigateId);
                }
            }
        }

        StageInvestigate stageInvestigate = new StageInvestigate();
        stageInvestigate.setUpdateTime(new Date());
        stageInvestigate.setStatus(1L);
        stageInvestigate.setId(stageInvestigateId);
        stageInvestigateMapper.updateStageInvestigate(stageInvestigate);
        stageInvestigateDetail.setFeedbackStatus(1L);

        return stageInvestigateDetailMapper.updateStageInvestigateDetail(stageInvestigateDetail);
    }

    /**
     * 批量删除阶段问卷细节
     *
     * @param ids 需要删除的阶段问卷细节主键
     * @return 结果
     */
    @Override
    public int deleteStageInvestigateDetailByIds(Long[] ids)
    {
        return stageInvestigateDetailMapper.deleteStageInvestigateDetailByIds(ids);
    }

    /**
     * 删除阶段问卷细节信息
     *
     * @param id 阶段问卷细节主键
     * @return 结果
     */
    @Override
    public int deleteStageInvestigateDetailById(Long id)
    {
        return stageInvestigateDetailMapper.deleteStageInvestigateDetailById(id);
    }

    @Override
    public List<StageInvestigateDetail> selectStageInvestigateDetailListByFeed() {
        return stageInvestigateDetailMapper.selectStageInvestigateDetailListByFeed();
    }

    @Override
    public void export(HttpServletResponse response,StageInvestigateDetail stageInvestigateDetail) throws IOException, TemplateException {
        Long id = stageInvestigateDetail.getId();
        StageInvestigateDetail detail = stageInvestigateDetailMapper.selectStageInvestigateDetailById(id);
        String studentName = detail.getStudentName();
        // 项目下的template路径
        String path = "ruoyi-modules/ruoyi-teaching/src/main/resources/";
       Map<String, Object> map = new HashMap<String, Object>();
        // 模板所在的路径
        map.put("tempFoldPath", path);
        // 生成的路径
        map.put("file", path + "/学员学习跟进月表.xls");
        // 模板名称
        map.put("tampPath", "学员学习跟进月表.ftl");
        // 最后生成的表格的名称
        map.put("excelName", "学员学习跟进情况-" + "Demo" + ".xls");
        // 封装数据
        Map<String, Object> exlParam = new HashMap<>();
        StageInvestigateDetail detail1 = stageInvestigateDetailMapper.selectStageInvestigateDetailById(id);
        exlParam.put("detail", detail1);
//        exlParam.put("detail", new StageInvestigateDetailServiceImpl().list(id));
        Student studentList = studentMapper.selectStudentStudentName(studentName);
        exlParam.put("studentList", studentList);
//        成绩（1到8周的成绩集合）

        List<ExamDetail> examDetailList=examDetailMapper.selectExamDetailByName(studentName);
        exlParam.put("examList",examDetailList);
//        exlParam.put("studentList", new StageInvestigateDetailServiceImpl().studentList(studentName));
        // 调用方法,返回浏览器访问的地址
        String downloadUrl = ExportExcelUtil.exportExcel(map, exlParam);
        //构造输出流
        String fileName ="ruoyi-modules/ruoyi-teaching/src/main/resources/学员学习跟进月表.xls";
        File file = new File(fileName);
        //将文件输出到response,返回给客户端

        FileInputStream in = new FileInputStream(file);
        byte[] buffer = new byte[in.available()];
        in.read(buffer);
        in.close();
        response.reset();
        response.addHeader("Content-Disposition", "attachment;filename=file.xls");
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/octet-stream");

        outputStream.write(buffer);
        FileUtils.deleteFolders("excel");
        outputStream.flush();
        outputStream.close();

    }

}
